//index.js
var Api = require('../../utils/api.js');
var util = require('../../utils/util.js');
//获取应用实例
var app = getApp()
Page({
  data: {
    motto: ' ',
    userInfo: {},
    logined: false
  },
  //事件处理函数
  bindViewTap: function () {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function () {
    console.log('onLoad')
    var that = this
    //调用应用实例的方法获取全局数据
    // app.getUserInfo(function(userInfo){
    //   //更新数据
    //   that.setData({
    //     userInfo:userInfo
    //   })
    // })
    this.data.logined = util.getUserId();
    console.log("index logined" + this.data.logined);
    if (this.data.logined) {
      //更新数据
      that.setData({
        userInfo: util.getUser(),
        logined: true
      })
    } else {
      that.setData({
        userInfo: {
          username: "",
          photo: "../../images/launch/launch.png"
        },
        logined: false
      })
    }

  },
  onPullDownRefresh: function () {
    console.log('下拉刷新', new Date());
    this.updateProfile();
  },
  onShow: function () {
    console.log('onShow')
    var that = this
    var logined = util.getUserId();
    console.log("index logined" + logined);
    if (logined) {
      //更新数据
      that.setData({
        userInfo: util.getUser(),
        logined: true
      })
    }
  },
  // 提交注册信息
  updateProfile: function (obj) {
    var self = this;
    console.log('submit - ');
    var logined = util.getUserId();
    console.log("index logined" + logined);
    if (!logined) {
      return;
    }
    wx.request({
      url: Api.personalInfo({ userId: logined }),
      header: Api.headers,
      success: function (res) {
        self.setData({
          disabled: false,
          loading: false
        });
        console.log(JSON.stringify(res));
        var data = res.data;

        if (data.code != 1 || !data.data) {

          wx.showModal({
            title: '提示',
            content: data.msg,
            showCancel: false,
            success: function (res) {
              if (res.confirm) {
                console.log('用户点击确定')
              }
            }
          });
          self.data.sended = false;
          return;
        }

        console.log("信息更新完成");
        wx.showToast({
          title: '信息更新完成',
          duration: 500
        })
        wx.setStorage({
          key: 'logined',
          data: 'done'
        });

        var user = wx.getStorageSync('usr');
        if (user) {
          // $api.rmStorage('usr');
        }
        user = {};
        user = data.data;

        console.log("setStorage" + "-" + JSON.stringify(user));
        wx.setStorage({
          key: 'user',
          data: user
        });

        //更新数据
        self.setData({
          userInfo: util.getUser(),
          logined: true
        })

      },
      fail: function (res) {
        self.setData({
          disabled: false,
          loading: false
        });
        self.data.sended = false;
        wx.showToast({
          title: '服务器异常<br>请稍后重试',
          icon: 'loading',
          duration: 500
        })
      },
      complete: function (res) {
        wx.stopPullDownRefresh()
        console.log('complete - ' + JSON.stringify(res));
      },
    });
  },
  //登录
  login: function (e) {
    var id = e.currentTarget.id,
      url = '../login/login?id=' + id;
    console.log('跳转登录' + id);
    wx.navigateTo({
      url: url
    })
  },
  //退出
  loginOut: function (e) {
    var self = this;
    wx.showModal({
      title: '提示',
      content: '确认退出！',
      success: function (res) {
        if (res.confirm) {
          console.log('用户点击确定')
          self.setData({
            userInfo: {
              username: "",
              photo: "../../images/launch/launch.png"
            },
            logined: false
          })
          wx.clearStorage();
        }
      }
    });

  },
  //跳转我的试用
  myTrial: function (e) {
    console.log('跳转我的试用');
    if (util.checkLogin("", "phone")) {
      var url = '../myTrial/myTrial';
      wx.navigateTo({
        url: url
      });
    }
  },
})
